﻿using DevComponents.DotNetBar;
using QLKetNoi;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLDVu.GUI
{
    public partial class DanhSachKhachHangForm : Office2007Form
    {
        public event EventHandler Button_Clicked;
        private readonly ThemBaoGiaForm form;
        private readonly QuanLyPhieuBaoGia formquanly;
        public DanhSachKhachHangForm(ThemBaoGiaForm form2)
        {
            InitializeComponent();
            this.form = form2;
        }
        public DanhSachKhachHangForm(QuanLyPhieuBaoGia form2)
        {
            InitializeComponent();
            this.formquanly = form2;
        }

        public static string NextID(string lastID, string prefixID)
        {
            int nextID = int.Parse(lastID.Remove(0, prefixID.Length)) + 1;
            int lengthNumerID = lastID.Length - prefixID.Length;
            string zeroNumber = "";
            for (int i = 1; i <= lengthNumerID; i++)
            {
                if (nextID < Math.Pow(10, i))
                {
                    for (int j = 1; j <= lengthNumerID - i; i++)
                    {
                        zeroNumber += "0";
                    }
                    return prefixID + zeroNumber + nextID.ToString();
                }
            }
            return prefixID + nextID;
        }

        void DanhSachKhachHang()
        {
            var dskhachhang = from kh in BO.DanhSachKhachHangBO.DanhSachKhachHang()
                              select new { kh.MaKhachHang, kh.TenKhachHang, kh.LOAIKHACHHANG.TenLoaiKhachHang, kh.DienThoai, kh.DiaChi, kh.Email, kh.KHUVUC.TenKhuVuc };
            //gvKhachHang.DataSource = dskhachhang;
        }
        private void DanhSachKhachHangForm_Load(object sender, EventArgs e)
        {
            DanhSachKhachHang();
            cbLoaiKhachHang.DataSource = BO.DanhSachKhachHangBO.DanhSachLoaiKhachHang();
            cbLoaiKhachHang.DisplayMember = "TenLoaiKhachHang";
            cbLoaiKhachHang.ValueMember = "MaLoaiKhachHang";

            cbKhuVuc.DataSource = BO.DanhSachKhachHangBO.DanhSachKhuVuc();
            cbKhuVuc.DisplayMember = "TenKhuVuc";
            cbKhuVuc.ValueMember = "MaKhuVuc";
            if (BO.DanhSachKhachHangBO.KhachHangCuoiCung() != null)
            {
                string t=BO.DanhSachKhachHangBO.KhachHangCuoiCung().MaKhachHang.Split(new Char[]{'0'})[0];
                txtMaKhachHang.Text = NextID(BO.DanhSachKhachHangBO.KhachHangCuoiCung().MaKhachHang, t);
            }
                
            else
                txtMaKhachHang.Text = "KH0001";
        }

        private void btnKhachHang_Click(object sender, EventArgs e)
        {
            
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            if (this.Button_Clicked != null)
            {
                this.Button_Clicked(sender, e);
            }
            this.Close();
        }

        private void gvKhachHang_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            //try
            //{
            //    int index = e.RowIndex;

            //    //ThemPhieuBaoGiaForm.makhachhang = gvKhachHang.Rows[index].Cells["MaKhachHang"].Value.ToString().Trim();
            //    QuanLyBaoGia.makhachhang = gvKhachHang.Rows[index].Cells["MaKhachHang"].Value.ToString().Trim();
            //}
            //catch
            //{
            //    MessageBox.Show("Không có khách hàng nào");
            //}
        }
        
        private void btnThem_Click(object sender, EventArgs e)
        {
            bool kt = false;
            if(txtTenKhachHang.Text.Trim()=="")
            {
                MessageBox.Show("Tên khách hàng không được để trống");
                kt = true;
                txtTenKhachHang.Focus();
                return;
            }
            if(txtDiaChi.Text.Trim()=="")
            {
                MessageBox.Show("Địa chỉ không được để trống");
                kt = true;
                txtDiaChi.Focus();
                return;
            }
            if(txtDienThoai.Text.Trim()=="")
            {
                MessageBox.Show("Điện thoại không được để trống");
                kt = true;
                txtDienThoai.Focus();
                return;
            }
           if(kt==false)
           {
               BO.DanhSachKhachHangBO.ThemKhachHang(txtMaKhachHang.Text.Trim(), txtTenKhachHang.Text.Trim(),
                cbLoaiKhachHang.SelectedValue.ToString(), txtDiaChi.Text.Trim(), cbKhuVuc.SelectedValue.ToString(),
                txtDienThoai.Text.Trim(), txtEmail.Text.Trim());
               DanhSachKhachHang();
              
               string makhachhang = txtMaKhachHang.Text.Split(new Char[] { '0' })[0];
               if (BO.DanhSachKhachHangBO.KhachHangCuoiCung(makhachhang) != null)
               {
                   string khcuoicung = BO.DanhSachKhachHangBO.KhachHangCuoiCung(makhachhang).MaKhachHang;
                   txtMaKhachHang.Text = NextID(khcuoicung, makhachhang);
               }
               else
               {
                   txtMaKhachHang.Text = makhachhang + "0001";
               }

               txtTenKhachHang.Text = "";
               txtDiaChi.Text = "";
               txtDienThoai.Text = "";
               txtEmail.Text = "";
              
               if(this.form!=null)
                this.form.LoadKhachHang();
               if (this.formquanly != null)
                   this.formquanly.LoadKhachHang();
               MessageBox.Show("Bạn đã thêm thành công");
           }
            

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThayDoiMa_Click(object sender, EventArgs e)
        {
            lblMaKhachHang.Text = "Bạn Hãy Nhập Mã Khách Hàng Mà Bạn Muốn";
            txtMaKhachHang.ReadOnly = false;
            txtMaKhachHang.Text = "";
            txtMaKhachHang.Focus();
        }

        private void txtMaKhachHang_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void txtMaKhachHang_KeyDown(object sender, KeyEventArgs e)
        {
             if (e.KeyCode == Keys.Enter)
             {
               
                 if(txtMaKhachHang.Text.Trim()=="")
                 {
                     if (BO.DanhSachKhachHangBO.KhachHangCuoiCung("KH") != null)
                     {
                         string khcuoicung = BO.DanhSachKhachHangBO.KhachHangCuoiCung("KH").MaKhachHang;
                         txtMaKhachHang.Text = NextID(khcuoicung, "KH");
                     }
                     else
                     {
                         txtMaKhachHang.Text = "KH" + "0001";
                     }


                     txtMaKhachHang.ReadOnly = true;
                     lblMaKhachHang.Text = "";
                     txtTenKhachHang.Focus();
                     
                 }
                 else
                 {
                     string makhachhang = txtMaKhachHang.Text.Split(new Char[] { '0' })[0];
                     if (BO.DanhSachKhachHangBO.KhachHangCuoiCung(makhachhang) != null)
                     {
                         string khcuoicung = BO.DanhSachKhachHangBO.KhachHangCuoiCung(makhachhang).MaKhachHang;
                         txtMaKhachHang.Text = NextID(khcuoicung, makhachhang);
                     }
                     else
                     {
                         txtMaKhachHang.Text = makhachhang + "0001";
                     }


                     txtMaKhachHang.ReadOnly = true;
                     lblMaKhachHang.Text = "";
                     txtTenKhachHang.Focus();
                 }
                 
             }
        }

        private void panelEx2_Click(object sender, EventArgs e)
        {

        }
    }
}
